<%@page language="java" contentType="text/html; charset=UTF-8" isELIgnored="false" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@taglib prefix="html" tagdir="/WEB-INF/tags" %>
<!DOCTYPE html>
<html>
<html:header title="待入库单">

    <link rel="stylesheet" href="${baseUrl}css/public.css" media="all">

    <style type="text/css">
    </style>

    <script>
        jQuery(function () {
            fixTableOperatorButton();
            ajaxSelects("${baseUrl}user/user/getAll.action.action",[
                {selectName:"userId",head:"全部" },
                {selectName:"makeUserId",head:"全部" }]);
            ajaxSelect("${baseUrl}organization/store/getStores.action","storeId","全部");

            ajaxSelect("${baseUrl}organization/house/listNormalVo.action","houseId","全部");

        });

        layui.use(['form', 'table','laydate'], function () {
            let $ = layui.jquery,
                form = layui.form,
                table = layui.table,
                laydate = layui.laydate,
                util = layui.util;

            laydate.render({
                elem: '#date',
                range:'~',
                done: function(value, date, endDate){
                    if (!emptyEntity(value)) {
                        $('[name="date1"]').val(getDateFromLayUiDate(date));
                        $('[name="date2"]').val(getDateFromLayUiDate(endDate));
                    } else {
                        $('[name="date1"]').val('');
                        $('[name="date2"]').val('');
                    }
                }
            });

            laydate.render({
                elem: '#makeTime',
                range:'~',
                done: function(value, date, endDate){
                    if (!emptyEntity(value)) {
                        $('[name="makeTime1"]').val(getDateFromLayUiDate(date));
                        $('[name="makeTime2"]').val(getDateFromLayUiDate(endDate));
                    } else {
                        $('[name="makeTime1"]').val('');
                        $('[name="makeTime2"]').val('');
                    }
                }
            });

            renderTable();


            table.on('tool(table)', function (obj) {
                let data = obj.data;
                switch(obj.event){
                    case 'in':
                        window.parent.openTableForSubIframe("新增入库单","${baseUrl}house/houseInNew.action?houseWaitInId="+data.id);
                        break;
                    case 'detail':
                        window.parent.openTableForSubIframe("待入库单单详情","${baseUrl}house/houseWaitInDetail.action?id="+data.id);
                        break;
                }
            });

            table.on('toolbar(table)', function (obj) {
                let data = table.checkStatus('table').data;
                switch(obj.event){
                    case 'export':
                        exportExcel(data);
                        break;
                };
            });

            form.on('submit(search)', function (data) {
                table.reload('table',{where: genSearch(),page: {curr: 1}});
                return false;
            });

        });

        function genSearchClose(noClose) {
            if (isTrue(noClose)) {
                return false;
            } else {
                return "";
            }
        }

        function exportExcel(data) {
            confirm("是否导出选择的单据?",function () {
                tipLoad("处理中，请稍后");
                post("${baseUrl}house/houseWaitIn/export.action"+ genParam(genSearch()),{},function (resp) {
                    closeAll();
                    if (resp.code == 1) {
                        downloadFileByForm("${baseUrl}file/excel/temp/"+resp.data);
                    }  else {
                        tip(resp.msg);
                    }
                });
            });

        }
        function clearSearch() {
            $("#searchForm")[0].reset();
            $('[name="date1"]').val('');
            $('[name="date2"]').val('');
            $('[name="makeTime1"]').val('');
            $('[name="makeTime2"]').val('');
        }

        function renderTable() {
            clearSearch();
            layui.table.render({
                id:'table',
                elem: '#table',
                toolbar: '#tableToolBar',
                defaultToolbar:[],
                url: "${baseUrl}house/houseWaitIn/getList.action",
                where : {
                    time:(new Date()).valueOf()
                },
                cols: [[
                    {type:'numbers'},
                    {width: 100, title: '业务日期',style:"font-size:12px;",templet: function (d) {
                            return GMTToDate(genLinOrder(d).date);
                        }},
                    {width: 100, title: '经手人',style:"font-size:12px;",templet: function (d) {
                            return genLinOrder(d).user.name;
                        }},
                    {field: 'no', width: 130, title: '单据编号',style:"font-size:12px;",templet: function (d) {
                            return genLinOrder(d).no;
                        }},
                    {field: 'no', width: 100, title: '类型',style:"font-size:12px;",templet: function (d) {
                            return genLinOrder(d).typeName;
                        }},
                    {width: 120, title: '往来单位名称',style:"font-size:12px;", templet: function (d) {
                            let linkUnit = genLinOrder(d).linkUnit;
                            return linkUnit==""?"":linkUnit.name;
                        }},
                    {width: 120, title: '门店',style:"font-size:12px;", templet: function (d) {
                            return d.houseVo.store.name;
                        }},
                    {width: 120, title: '入库仓库',style:"font-size:12px;", templet: function (d) {
                            return d.houseVo.name;
                        }},
                    {field:'number', width: 100, title: '待入库量',style:"font-size:12px;", templet: function(d){
                            return genWaitInGoodsNumber(d.houseWaitInGoodsVoList,d.houseWaitInPacketVoList);
                        }},
                    {minWidth: 150, title: '入库商品',style:"font-size:12px;", templet: function(d){
                            return genGoodsAndPacket(d.houseWaitInGoodsVoList,d.houseWaitInPacketVoList);
                        }},
                    {title: '操作', minWidth: 80,fixed:'right', templet: '#operBar'},

                ]],
                limit: 10,
                page: { //支持传入 laypage 组件的所有参数（某些参数除外，如：jump/elem） - 详见文档
                    layout: ['limit', 'count', 'prev', 'page', 'next', 'skip'] //自定义分页布局
                    //,curr: 5 //设定初始在第 5 页
                    ,groups: 5 //只显示 5 个连续页码
                    ,first: false //不显示首页
                    ,last: false //不显示尾页
                },
                done: function(res, curr, count){
                }
            });

        }

        function genWaitInGoodsNumber(goodsList, packetList) {
            let number = 0;
            if (!emptyList(goodsList)) {
                $.each(goodsList, function(i,vo){
                    number += vo.waitInNumber;
                });
            }

            if (!emptyList(packetList)) {
                $.each(packetList, function(i,vo){
                    number += vo.waitInNumber;
                });
            }
            return number;
        }

        //获取关联单结构体
        function genLinOrder(d) {
            let vo = {};

            if (d.type ==1) {
                vo = d.inGoodsVo;
                vo.linkUnit = d.inGoodsVo.provider;
                vo.typeName = "进货";
                return vo;
            }
            if (d.type ==2) {
                vo = d.refundVo;
                vo.linkUnit = d.refundVo.custom;
                vo.typeName = "销售退货";
                return vo;
            }

            if (d.type ==3) {
                vo = d.goodsTransferVo;
                vo.linkUnit = "";
                vo.typeName = "调拨";
                return vo;
            }
            if (d.type ==4) {
                vo = d.borrowInVo;
                vo.linkUnit = d.borrowInVo.provider;
                vo.typeName = "借入";
                return vo;
            }
            if (d.type ==5) {
                vo = d.borrowOutBackVo;
                vo.linkUnit = d.borrowOutBackVo.custom;
                vo.typeName = "借出归还";
                return vo;
            }
        }

        function genSearch() {
            let json = getFormJson($("#searchForm"));
            return json;
        }
    </script>

</html:header>
<body style="background-color: #ffffff;overflow: auto;">
    <form class="layui-form layui-form-pane" action="" id="searchForm">

        <input type="hidden" name="date1">
        <input type="hidden" name="date2">
        <input type="hidden" name="makeTime1">
        <input type="hidden" name="makeTime2">

        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">其他信息</label>
                <div class="layui-input-inline">
                    <input type="text" id="search" name="search" placeholder="编号、往来单位、商品、备注" class="layui-input">
                </div>
            </div>

            <div class="layui-inline">
                <a class="layui-btn" lay-submit="" lay-filter="search" id="btn-search"><i class="layui-icon layui-icon-search"></i></a>
                <a class="layui-btn" id="selectSearch">筛选</a>
            </div>

            <div class="table-tool-div-screen" status="close" style="display: none">


                <div class="layui-inline">
                    <label class="layui-form-label">制单日期</label>
                    <div class="layui-input-inline">
                        <input type="text" id="makeTime" placeholder="请选择范围" class="layui-input" autocomplete="off">
                    </div>
                </div>

                <div class="layui-inline">
                    <label class="layui-form-label">单据编号</label>
                    <div class="layui-input-inline">
                        <input type="text" name="no" class="layui-input">
                    </div>
                </div>

                <div class="layui-inline" >
                    <label class="layui-form-label">经手人</label>
                    <div class="layui-input-inline">
                        <select name="userId"></select>
                    </div>
                </div>

                <div class="layui-inline" >
                    <label class="layui-form-label">入库状态</label>
                    <div class="layui-input-inline">
                        <select name="status">
                            <option value="">全部</option>
                            <option value="1">未入库</option>
                            <option value="2">部分入库</option>
                        </select>
                    </div>
                </div>

                <div class="layui-inline">
                    <label class="layui-form-label">业务日期</label>
                    <div class="layui-input-inline">
                        <input type="text" id="date" placeholder="请选择范围" class="layui-input" autocomplete="off">
                    </div>
                </div>

                <div class="layui-inline">
                    <label class="layui-form-label">商品名称</label>
                    <div class="layui-input-inline">
                        <input type="text" name="goodsName" class="layui-input">
                    </div>
                </div>

                <div class="layui-inline">
                    <label class="layui-form-label">备注</label>
                    <div class="layui-input-inline">
                        <input type="text" name="remark" class="layui-input">
                    </div>
                </div>

                <div class="layui-inline">
                    <label class="layui-form-label">往来单位</label>
                    <div class="layui-input-inline">
                        <input type="text" name="unitName" class="layui-input">
                    </div>
                </div>

                <div class="layui-inline" >
                    <label class="layui-form-label">门店</label>
                    <div class="layui-input-inline">
                        <select name="storeId"></select>
                    </div>
                </div>

                <div class="layui-inline" >
                    <label class="layui-form-label">仓库</label>
                    <div class="layui-input-inline">
                        <select name="houseId"></select>
                    </div>
                </div>

                <div class="layui-inline" >
                    <label class="layui-form-label">制单人</label>
                    <div class="layui-input-inline">
                        <select name="makeUserId"></select>
                    </div>
                </div>

                <div class="layui-inline" >
                    <label class="layui-form-label">类型</label>
                    <div class="layui-input-inline">
                        <select name="type">
                            <option value="">全部</option>
                            <option value="1">进货</option>
                            <option value="2">销售退货</option>
                            <option value="3">调拨</option>
                            <option value="4">借入</option>
                            <option value="5">借出归还</option>
                        </select>
                    </div>
                </div>

                <div class="layui-form-item">
                    <a class="layui-btn" lay-submit="" lay-filter="search">搜索</a>
                    <a class="layui-btn layui-btn-primary" id="clearSearch">清空所有条件</a>
                </div>
            </div>
            <script>

                $("#selectSearch").click(function () {
                    if ($(".table-tool-div-screen").attr("status")=="close") {
                        $(".table-tool-div-screen").css("display","block");
                        $(".table-tool-div-screen").attr("status","open");
                        $("#search").parent().parent().css("display","none");
                        $("#search").val("");
                        $("#btn-search").css("display","none");
                        $(this).html("收起");
                    } else {
                        $(".table-tool-div-screen").css("display","none");
                        $(".table-tool-div-screen").attr("status","close");
                        $("#search").parent().parent().css("display","inline-block");
                        $("#btn-search").css("display","inline-block");
                        $(this).html("筛选");
                    }
                });

                $("#clearSearch").click(function () {
                    clearSearch();
                });

            </script>
        </div>
    </form>
    <hr/>

    <table class="layui-hide" id="table" lay-filter="table" lay-size="sm"></table>
<script type="text/html" id="tableToolBar">
    <div class="layui-inline">
        <a class="layui-btn layui-btn-primary" lay-event="export">导出</a>
    </div>
<%--    <div class="layui-inline">--%>
<%--        <a class="layui-btn layui-btn-primary" lay-event="batchIn">批量入库</a>--%>
<%--    </div>--%>
</script>

<script type="text/html" id="operBar">
    <a class="layui-btn layui-btn-xs table-operator" lay-event="in">入库</a>
    <a class="layui-btn layui-btn-xs layui-btn-normal table-operator" lay-event="detail">详情</a>
</script>
</body>
<html:loading/>
</html>